﻿function CalendarController(fullCalendarController) {

    this.OnChange = undefined;

    this.UI = new Object();
    this.UI.Holder = $(fullCalendarController);
    this.UI.CalendarPicker = this.UI.Holder.find('.calendar');
    this.UI.DisplayInput = this.UI.Holder.find('input[type=text]');
    this.UI.ValueInput = this.UI.Holder.find('input[type=hidden]');

    this.CurrentDate = new Date();
    this.CurrentDate = $.datepicker.parseDate(this.FormatValue, this.UI.ValueInput.val());
    if (!this.CurrentDate)
        this.CurrentDate = new Date();

    this.Initialize();
};
CalendarController.prototype.Initialize = function () {
    var controller = this;
    this.UI.DisplayInput.attr('readonly', 'readonly').css('cursor', 'pointer');
    this.UI.DisplayInput.click(function () {
        if (controller.UI.CalendarPicker.is('*:visible'))
            controller.UI.CalendarPicker.hide();
        else {
            $('.calendar').hide();
            controller.UI.CalendarPicker.show();
        }
    });
    this.UI.CalendarPicker.datepicker({
        defaultDate: this.CurrentDate,
        onSelect: function (dateText, inst) {
            controller.UI.CalendarPicker.hide();
            controller.SetDate(controller.UI.CalendarPicker.datepicker('getDate'), true);
        }
    });
    this.SetDate(this.CurrentDate, true);
};
CalendarController.prototype.SetDate = function (date, sendFromDatePicker) {
    if (!sendFromDatePicker)
        this.UI.CalendarPicker.datepicker('setDate', date);
    this.UI.DisplayInput.val($.datepicker.formatDate(this.FormatDisplay, date));
    this.UI.ValueInput.val($.datepicker.formatDate(this.FormatValue, date));
    this.CurrentDate = date;
    if (this.OnChange)
        this.OnChange();
};

CalendarController.prototype.FormatDisplay = 'd MM \'\'y';
CalendarController.prototype.FormatValue = 'dd.mm.yy';

function CalendarDatesObserver(arrivalController, departureController) {
    this.Arrival = arrivalController;
    this.Departure = departureController;

    this.Arrival.UI.CalendarPicker.datepicker('option', 'minDate', new Date()); // ustawiamy minimalny zakres od dzisiaj na przyjazd

    var observer = this;
    this.Arrival.OnChange = function () { observer.ArrivalHasChanged(); };
    this.Departure.OnChange = function () { observer.DepartureHasChanged(); };

    var markRange = function (date) { return observer.MarkRange(date) };
    this.Arrival.UI.CalendarPicker.datepicker('option', 'beforeShowDay', markRange);
    this.Departure.UI.CalendarPicker.datepicker('option', 'beforeShowDay', markRange);
    this.ArrivalHasChanged();
    this.DepartureHasChanged(); // wyzwalamy początkowe ograniczenie na wyjazd. Automatycznie zablokuje to ustawienie dany mniejszej niż przyjazd
};
CalendarDatesObserver.prototype.ArrivalHasChanged = function () {
    if (this.Arrival.CurrentDate >= this.Departure.CurrentDate)
        this.Departure.SetDate(new Date(this.Arrival.CurrentDate.getFullYear(), this.Arrival.CurrentDate.getMonth(), this.Arrival.CurrentDate.getDate() + 1));
    else
        this.Departure.UI.CalendarPicker.datepicker('option', 'minDate', new Date(this.Arrival.CurrentDate.getFullYear(), this.Arrival.CurrentDate.getMonth(), this.Arrival.CurrentDate.getDate() + 1));
    this.Arrival.UI.CalendarPicker.datepicker('refresh');
    this.Departure.UI.CalendarPicker.datepicker('refresh');
};
CalendarDatesObserver.prototype.DepartureHasChanged = function () {
    this.Arrival.UI.CalendarPicker.datepicker('refresh');
    this.Departure.UI.CalendarPicker.datepicker('refresh');
};
CalendarDatesObserver.prototype.MarkRange = function (date) {
    var cssclass = '';
    if (date >= this.Arrival.CurrentDate && date <= this.Departure.CurrentDate)
        cssclass = 'ui-datepicker-activerange';
    return [true, cssclass];
};


$(function () {
    var arrivalController = new CalendarController($('.arrival'));
    var departureController = new CalendarController($('.departure'));

    var changesObserver = new CalendarDatesObserver(arrivalController, departureController);

    // galeria
    $('a.hotelGallery').click(function (e) {
        e.preventDefault();
        var images = new Array();
        $('#hotelGallerySet a').each(function () {
            images.push([$(this).attr('href')]);
        });
        $.slimbox(images, 0, { counterText: 'Zdjęcie nr. {x} z {y}' });
    });
});